package com.sjz.filter;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * 1、传参数时设置utf-8格式  url.append(URLEncoder.encode(entry.getValue(), ecoding));
 * 2、把得到的值进行utf-8设置     value = new String(value.getBytes("ISO8859-1"), "UTF-8");
 * 这里用filter来进行统一处理
 *  get请求乱码编码处理
 *  
 *  post请求设置 
 *  一、请求的时候设置编码 
 *  二、相应的时候 设置编码  req.setCharacterEncoding("UTF-8");
 */
public class EncodingFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		if("GET".equals(req.getMethod())){
			EncodingRequestWrapper wrapper = new EncodingRequestWrapper(req);
			chain.doFilter(wrapper, response);
		}else{
			req.setCharacterEncoding("UTF-8");
			chain.doFilter(request, response);
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
